import { t } from 'i18next'
import requestMeeting, { request } from '@/plugins/axios'
import Alert from '@/components/Alert'
import { meetingInfoSetModal } from './translator'
import dayjs from '@/plugins/dayjs'
/** 设置会议信息 */
export const fetchSetMeetingInfo = async (meetingMoid: string, e164: string, param: meetingInfoSetModal) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/meeting_info`,
		method: 'post',
		data: JSON.stringify(param),
	})
		.then((res) => {
			if (!res.success) {
				if (res.error_code === 51125) {
					Alert(t('该会议名称已存在'))
				} else {
					Alert(t('会议信息设置失败'))
				}
			} else {
				return true
			}
		})
		.catch(() => {
			Alert(t('会议信息设置失败'))
		})
/** 模板立即召开名称校验 true-不存在 false-已存在*/
export const fetchCheckMeetingName = async (params: {
	meeting_id?: string
	conf_name: string
	end_time?: string
	start_time?: string
}) => {
	let end_time = dayjs(params.end_time).tz().format()
	let start_time = dayjs(params.start_time).tz().format()
	return request<{ result: number }>({
		url: '/valid_conf_name',
		method: 'get',
		params: { ...params, end_time, start_time },
	}).then((res) => (res.success && res.result === 1 ? true : false))
}

/** 计算会议剩余时间 */
export const fetchCalcMinute = async (meetingMoid: string) =>
	requestMeeting<{ time: string }>({
		url: `/mcc/${meetingMoid}/calc_minute`,
		method: 'get',
	}).then((res) => res)

/** 是否可以延长会议 */
export const fetchDelayCheck = async (meetingMoid: string, delay: number) =>
	requestMeeting<{ delay_meeting: number }>({
		url: `/mcc/${meetingMoid}/delay_check`,
		params: { delay: delay },
		method: 'get',
	})
		.then((res) => res)
		.catch((err) => err)

/** 延长会议 */
export const fetchDelayMeeting = async (meetingMoid: string, delay: number) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/delay_meeting`,
		method: 'post',
		data: JSON.stringify({ delay: delay }),
	})
		.then((res) => res)
		.catch((err) => err)

/** 不延长会议 */
export const fetchNoDelayMeeting = async (meetingMoid: string) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/delay_meeting`,
		method: 'DELETE',
	})
		.then((res) => res)
		.catch((err) => err)
